package lesson2;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;

/**
 * Created by Виталий on 23.03.14.
 */
public class Homework4_2 {

    private WebDriver driver;
    private StringBuffer verificationErrors = new StringBuffer();
    private String baseUrl = "http://rozetka.com.ua/";

    @BeforeClass
    public void setUp() throws Exception {
        // Create a new instance of the Firefox driver
        driver = new FirefoxDriver();
        driver.get(baseUrl);
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
    }
    /*
    @DataProvider(name = "test1")
    public Object[][] createData1() {
        return new Object[][] {
                { "Name", new String("Vitalii") },
                { "Email", new String("vitalii@gmail.com")},
                { "Password", new String("12345")},
        };
    }*/
    @DataProvider(name = "test1")
    public Object[][] createData1() {
        return new Object[][] {
                { "dgdfg", "sdfsdfdfsvxczqwe@gmail.com", "asd" },
        };
    }
    @Test(dataProvider = "test1")
    public void test(String Name, String Email, String Password) throws Exception {
        try {
            WebElement enter =  driver.findElement(By.xpath("//*[@id='user_menu']/div/a"));
            enter.click();

            WebElement regist = driver.findElement(By.xpath("/html/body/header/div/div/div/div[3]/div[3]/div/div/div/form/div[5]/a"));
            regist.click();

            WebElement inputTitle = driver.findElement(By.name("title"));
            inputTitle.sendKeys(Name);

            WebElement inputEmail = driver.findElement(By.name("email"));
            inputEmail.sendKeys(Email);

            WebElement inputPassword = driver.findElement(By.name("password"));
            inputPassword.sendKeys(Password);

            WebElement sumbitRegistration = driver.findElement(By.className("button-css-green"));
            sumbitRegistration.click();

            WebElement accountTitle = driver.findElement(By.className("personal-info-title"));
            assertEquals(accountTitle.getText(), "Мои настройки");

        } catch (Error e) {
            verificationErrors.append(e.toString());
        }
    }

    @AfterClass
    public void tearDown() throws Exception {
        //Close the browser
        driver.quit();
        String verificationErrorString = verificationErrors.toString();
        if (!"".equals(verificationErrorString)) {fail(verificationErrorString);
        }
    }
}
